﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CUREEntity;
using System.Data;
using DBUtility;
using CUREEntity.SystemManagement;

namespace CUREDataAccessLayers
{
    public class EventLogDal
    {  /// <summary>
        /// 向EVENTLOG日志表添加数据  
        /// </summary>
        /// <param name="eventlog"></param>
        /// <returns></returns>
        public bool InsertEventLogin(EventLogModel eventlog)
        {
            StringBuilder sql = new StringBuilder();
            int num = 0;
            bool result = false;
            sql.Append("insert into eventlog t values('" + eventlog.Id + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo) + "','" + eventlog.Message + "','" + eventlog.Masterid + "')");
            num = DbHelperOra.ExecuteSql(sql.ToString());
           // num = this.DbHelperInstance.ExecuteNonQuery(sql);

            if (num > 0)
            {
                result = true;
            }
            return result;
        }
        /// <summary>
        /// 获取日志信息信息
        /// </summary>
        /// <param name="where">字典参数</param>
        /// <param name="pageIndex">当前页的索引</param>
        /// <param name="pageSize">每页显示的条数</param>
        /// <param name="totalRecordCount">总记录数</param>
        /// <param name="totalPageCount">总页数</param>
        /// <returns></returns>
        public DataSet GetEventLogData(IDictionary<string, object> where, int pageIndex, int pageSize, out int totalRecordCount, out int totalPageCount)
        {
            //IList<EventLogEntity> list = null;
            DataSet ds = new DataSet();
            StringBuilder sql = new StringBuilder();
            StringBuilder sqlTotal = new StringBuilder();
            string type = string.Empty;
            string startdate = string.Empty;
            string enddate = string.Empty;
            #region 处理变量
            if (where.ContainsKey("type") && where["type"] != null && where["type"].ToString() != "")
            {
                type = where["type"].ToString().Trim();
            }
            if (where.ContainsKey("startdate") && where["startdate"] != null && where["startdate"].ToString() != "")
            {
                startdate = where["startdate"].ToString().Trim();
            }
            if (where.ContainsKey("enddate") && where["enddate"] != null && where["enddate"].ToString() != "")
            {
                enddate = where["enddate"].ToString().Trim();
            }
            #endregion
            sql.Append("select * from ( ");
            sql.Append("select rownum as rid, t.id, t.yyyymmddhhmmss,t.message,m.name as masterid from eventlog t left  join master m on t.masterid=m.id where 1=1 ");
            sqlTotal.Append("select count(*) from eventlog t left  join master m on t.masterid=m.id where 1=1");
            if (type == "1")
            {
                sql.Append(" and substr(YYYYMMDDHHMMSS, 1,10)='" + startdate + "'");
                sqlTotal.Append(" and substr(YYYYMMDDHHMMSS, 1,10)='" + startdate + "'");
            }
            if (type == "2")
            {
                sql.Append(" and substr(YYYYMMDDHHMMSS, 1,10)>='" + startdate + "'");
                sql.Append(" and substr(YYYYMMDDHHMMSS, 1,10)<='" + enddate + "'");
                sqlTotal.Append(" and substr(YYYYMMDDHHMMSS, 1,10)>='" + startdate + "'");
                sqlTotal.Append(" and substr(YYYYMMDDHHMMSS, 1,10)<='" + enddate + "'");
            }
            sql.Append(" and rownum<=" + pageIndex * pageSize + ") table_tmp ");
            sql.Append(" where table_tmp.rid>" + (pageIndex - 1) * pageSize);
            
            try
            {
                totalRecordCount = Convert.ToInt32(DbHelperOra.QueryTable(sqlTotal.ToString()).Rows[0][0].ToString());
                totalPageCount = (totalRecordCount + pageSize - 1) / pageSize;
                ds = DbHelperOra.Query(sql.ToString());
                //list = this.DbHelperInstance.GetPagedData<EventLogEntity>(pageIndex, pageSize, sql, " yyyymmddhhmmss desc ", out totalRecordCount, out totalPageCount);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }


        /// <summary>
        /// 插入日志信息
        /// </summary>
        /// <param name="where">字典参数</param>
        /// <returns>true 插入成功/false 插入失败</returns>
        public bool InsertEventLog(IDictionary<string, object> where)
        {
            bool result = false;
            StringBuilder sql = new StringBuilder();
            string id = GetMaxId().ToString();
            string msg = string.Empty;
            string masterid = string.Empty;
            #region 处理数据
            if (where.ContainsKey("msg") && where["msg"] != null && where["msg"].ToString() != "")
            {
                msg = where["msg"].ToString().Trim();
            }
            if (where.ContainsKey("masterid") && where["masterid"] != null && where["masterid"].ToString() != "")
            {
                masterid = where["masterid"].ToString().Trim();
            }
            #endregion
            sql.Append("insert into eventlog t values('" + id + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo) + "','" + msg + "','" + masterid + "')");
            try
            {
                int i = DbHelperOra.ExecuteSql(sql.ToString());
                //int i = this.DbHelperInstance.ExecuteNonQuery(CommandType.Text, sql);
                if (i == 1)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }


        /// <summary>
        /// 获取最大的id
        /// </summary>
        /// <returns>返回最大的id+1</returns>
        public int GetMaxId()
        {
            int maxid = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select max(to_number(id))+1 from eventlog t");
                maxid = DbHelperOra.GetMaxID("id", "eventlog");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return maxid;
        }
    }
}
